package com.baize.baizeapp.test

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Rect
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.PixelCopy
import android.view.View
import android.widget.ImageView
import com.baize.baizeapp.R
import com.baize.baizeapp.test.mohu.FastBlurUtil
import com.baize.baizeapp.test.mohu.ScreenShotImageHelper
import com.baize.baizeapp.test.mohu.TranslacentActivity

class MohuActivity : AppCompatActivity() {

    companion object {
        fun open(context: Context) {
            context.startActivity(
                Intent(context, MohuActivity::class.java)
            )
        }
    }

    lateinit var iv_show: ImageView

    var scaleRadio = 50
    var blurRadius = 8

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mohu)
        iv_show = findViewById<ImageView>(R.id.iv_show)

    }

    fun showScreen(v: View) {
        Log.i("yanze", "is DrawingCacheEnabled : ${window.decorView.isDrawingCacheEnabled}")
        window.decorView.isDrawingCacheEnabled = true
        window.decorView.buildDrawingCache()
        val bmp: Bitmap? = window.decorView.drawingCache
        bmp?.let {
            //1. bitmap     2. 缩放比例，一般是10分之一  3. 边缘平滑
            Log.i("yanze", "original size:${it.byteCount / 1024}")
            val scaleBitmap = Bitmap.createScaledBitmap(
                it,
                bmp.width / scaleRadio,
                bmp.height / scaleRadio,
                false
            )
            Log.i("yanze", "scale size:${scaleBitmap.byteCount / 1024}")
//            iv_show.setImageBitmap(scaleBitmap)
            iv_show.setImageBitmap(FastBlurUtil.doBlur(scaleBitmap, blurRadius, false))
        }
        //销毁缓存
        iv_show.postDelayed({
            iv_show.setImageBitmap(null)
            window.decorView.destroyDrawingCache()
            window.decorView.isDrawingCacheEnabled = false
        }, 2000)
    }

    fun showScreen2(v: View) {
        val bitmap = getBitmapFromView(window.decorView,scaleRadio)
//        iv_show.setImageBitmap(scaleBitmap)
        iv_show.setImageBitmap(FastBlurUtil.doBlur(bitmap, blurRadius, false))
    }

    fun showScreen3(v: View) {
        getBitmapFromView(window.decorView, this) {
            val scaleBitmap =
                Bitmap.createScaledBitmap(it, it.width / scaleRadio, it.height / scaleRadio, false)
            Log.i("yanze", "size:${it.byteCount / 1024}  scale:${scaleBitmap.byteCount / 1024}")
//            iv_show.setImageBitmap(scaleBitmap)
            iv_show.setImageBitmap(FastBlurUtil.doBlur(scaleBitmap, blurRadius, false))
        }
    }

    fun fabu(v: View) {
        ScreenShotImageHelper.generateScreenBlurBg(this)
        val bitmap = FastBlurUtil.doBlur(getBitmapFromView(window.decorView,20),8,false)
        TranslacentActivity.open(this,bitmap)
    }

    /**
     * 获取ViewBitmap
     * @param scale 缩放比
     */
    fun getBitmapFromView(view: View, scale: Int): Bitmap {
        val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        view.draw(canvas)
        val scaleBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.width / scale, bitmap.height / scale, false)
        Log.i("yanze", "size:${bitmap.byteCount / 1024}  scale：${scaleBitmap.byteCount / 1024}")
        return scaleBitmap
    }

    fun getBitmapFromView(view: View, activity: Activity, callback: (Bitmap) -> Unit) {
        activity.window?.let { window ->
            val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
            val locationOfViewInWindow = IntArray(2)
            view.getLocationInWindow(locationOfViewInWindow)
            try {
                PixelCopy.request(
                    window,
                    Rect(
                        locationOfViewInWindow[0],
                        locationOfViewInWindow[1],
                        locationOfViewInWindow[0] + view.width,
                        locationOfViewInWindow[1] + view.height
                    ),
                    bitmap,
                    { copyResult ->
                        if (copyResult == PixelCopy.SUCCESS) {
                            callback(bitmap)
                        }
                        // possible to handle other result codes ...
                    },
                    Handler()
                )
            } catch (e: IllegalArgumentException) {
                // PixelCopy may throw IllegalArgumentException, make sure to handle it
                e.printStackTrace()
            }
        }
    }

}